'''
Created on Feb 7, 2012

@author: Michael Lawson

This module creates the data structures needed to perform the BEMT calculations
'''
from ConfigParser import RawConfigParser
import numpy

class Simulation(object): 
    def __init__(self,caseDir):
        # Identify the directories 
        self.caseDir = caseDir
        self.inputDir = caseDir + '/inputFiles'
        self.outputDir = caseDir + '/outputFiles'
        self.simData = self.inputDir + '/simData'

        # Load the simulation file
        cfg = RawConfigParser()
        cfg.read(self.simData)
        
        # Get the simulation parameters
        self.simName = cfg.get('Simulation', 'Simulation name')
        self.maxItr = cfg.getint('Simulation', 'Max number of iterations')
        self.indTol = cfg.getfloat('Simulation', 'Error tolerance for induction iteration')
        
        # Get the simulation cases     
        self.cases = Cases(self.inputDir)
        
    def Disp(self):
        print 'Simulation properties'
        print '\tCase directory =', self.caseDir
        print '\tSimulation name =', self.simName
        
class Cases(object):
    def __init__(self,inputDir):
        self.simCases = inputDir + '/simCases'
        cases = numpy.loadtxt(self.simCases) 
        self.U = cases[:,0]
        self.omega = cases[:,1]
        self.pitch = cases[:,2]
        
class Fluid(object): 
    def __init__(self,dataFile):     
        cfg = RawConfigParser()
        cfg.read(dataFile)
        self.rho = cfg.getfloat('Fluid', 'Fluid density')
        self.nu = cfg.getfloat('Fluid', 'Fluid kinematic viscosity')
      
    def Disp(self):
        print 'Fluid properties'
        print '\tFluid density =', self.rho
        print '\tFluid kinematic viscosity =', self.nu
        
class Rotor(object): 
    def __init__(self,inputDir):
        self.inputDir = inputDir
        cfg = RawConfigParser()
        cfg.read(''.join([self.inputDir+'/simData']))
        self.R = cfg.getfloat('Rotor', 'Rotor radius')
        self.hubR = cfg.getfloat('Rotor', 'Hub radius')
        self.nB = cfg.getint('Rotor', 'Number of blades')
        self.blade = Blade(self.inputDir)
        self.blade.r_R = self.blade.r/self.R
    
    def TsrCalc(self,):
        pass
        
    def Disp(self):
        print 'Rotor properties'
        print '\tRotor radius =', self.R
        print '\tHub radius =', self.hubR
        print '\tNumber of blades =', self.nB
        print '\tBlade properities'
        print '\t\tNumber of blade elements =', self.blade.nElem

        
class Blade(object): 
    def __init__(self,inputDir):   
        self.inputDir = inputDir  
        self.bladeDefFile = self.inputDir+'/bladeDef'
        self.airfoilDefFile = self.inputDir+'/airfoilDef'
        self.airfoilDataDir = self.inputDir+'/airfoilData'
        
        cfg = RawConfigParser()
        cfg.read(self.inputDir+'/simData')
        
        blade = numpy.loadtxt(self.bladeDefFile) 
        self.r = blade[:,0]
        self.chord = blade[:,1]
        self.preTwist = blade[:,2]
        self.airfoil = blade[:,3]
        self.nElem = numpy.size(self.chord,0)
        self.elemLen = self.r[0] - self.r[1] # This assumes that the element lengths are all the same size
        self.r_R = 999*numpy.ones((numpy.size(self.r,0),1))
        self.t = 999*numpy.ones((numpy.size(self.r,0),1))
        
        
        afDefFile = open(self.airfoilDefFile)
        self.airfoilFiles = afDefFile.readlines()
        for i in range(0,numpy.size(self.airfoilFiles,0)):
            temp = self.airfoilFiles[i]
            self.airfoilFiles[i] = temp[:-1]
        
        